package com.moleq.network;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;

import com.moleq.util.BigDecmialTool;
import com.moleq.util.MoleQUtil;

public class InventoryReport {

	private List<String[]> m_report = new ArrayList<String[]>();

	private int width = 42;
	private String mReportName = "";
	
	private static final int AMT = 10;
	private static final int NAME = 24;
	private static final int QTY = 12;
	
	
	public InventoryReport(String reportName) {
		this.setmReportName(reportName);
	}

	public void sum(String key, int column, String value) {
		int idx = -1;
		String[] row = null;

		idx = findIndexByKey(key);

		if (idx >= 0) {
			row = m_report.get(idx);

			if (column < row.length) {
				String val1 = row[column];
				if (isDouble(val1) && isDouble(value)) {
					row[column] = String.valueOf(Double.valueOf(val1) + Double.valueOf(value));
				}
				else if(!value.trim().equals("")){
					row[column] = value;
				}
			}
		}
	}
	
	public void sum(String[] line) {
		int idx = -1;
		if (line != null) {
			String key = line[0];
			if (isExist(key)) {
				idx = findIndexByKey(key);
				String[] row = null;
				if (idx >= 0) {
					row = m_report.get(idx);
					for (int i = 1; i < line.length; i++) {
						if (row[2].trim().equals("")) {
							row[2] = "0";
						}
						sum(key, i, line[i]);
						
						
					}
				}
			} else {
				addLine(line);
			}
		}
	}

	public void update(String key, int column, String value) {
		int idx = -1;
		String[] row = null;

		idx = findIndexByKey(key);

		if (idx >= 0) {
			row = m_report.get(idx);

			if (column < row.length) {
				row[column] = value;
			}
		}
	}

	public void update(String[] line) {
		int idx = -1;
		if (line != null) {

			String key = line[0];
			if (isExist(key)) {
				idx = findIndexByKey(key);

				String[] row = null;
				if (idx >= 0) {
					row = m_report.get(idx);
					for (int i = 1; i < line.length; i++) {

						update(key, i, line[i]);
					}
				}
			} else {
				addLine(line);
			}
		}
	}

	private boolean isExist(String key) {
		boolean result = false;
		int i = findIndexByKey(key);
		if (i >= 0) {
			result = true;
		}
		return result;
	}

	private void addLine(String[] line) {
		if (m_report != null) {
			m_report.add(line);
		}
	}
	
	public String cutPoint(String line){
		
		String ls = line;
		String ls2 = ls.substring(0, 1);
		if (ls2.equals("'")) {
			ls = ls.substring(1, ls.length() - 1);
		}
		
		return ls;
	}

	public int findIndexByKey(String key) {
		int idx = -1;
		for (int i = 0; i < m_report.size(); i++) {
			String[] line = m_report.get(i);

			if (line.length > 0) {
				String id = line[0];
				if (id.equals(key)) {
					idx = i;
					break;
				}
			}
		}

		return idx;
	}

	public void reset(){
		if (m_report !=null) {
			m_report.clear();
		}else{
			m_report = new ArrayList<String[]>();
		}
	}
	
	public void calculateAverage(){
		double netSale = 0,count= 0;
		String average = "";
		int idx = 0;
		if (m_report.size() == 0 )return;
		idx = findIndexByKey("02");
		if(idx > 0)netSale = Double.valueOf(m_report.get(idx)[3]);
		idx = findIndexByKey("11");
		if(idx > 0)count = Double.valueOf(m_report.get(idx)[2]);
		idx = findIndexByKey("12");
		if(idx > 0){
			average = String.valueOf(BigDecmialTool.div(netSale, count)) ;
			
			update("12",3,average);
		}
	}
	
	public String getReportName(String startDate){
		
		String result ="";
		result += SetSpaceE(NAME, mReportName);
		result += SetSpaceB(QTY, "");
		result += SetSpaceB(AMT, MoleQUtil.fixDateTime2(startDate, "yyyy-MM-dd", "yyyyMMdd"));
		result += "\n";
		
		return result;
	}
	
	public String getReportDetailName(String name){
		
		String result ="";
		result += SetSpaceE(NAME, name);
		result += SetSpaceB(QTY, "");
		result += SetSpaceB(AMT,"");
		result += "\n";
		
		return result;
	}
	
	public String getTotlal(boolean showQty){
		String result = "";
		String price,qty;
		double total = 0,iQty = 0;
		int totalqty = 0;
		
		for (int i = 0; i < m_report.size(); i++) {
			String[] line = m_report.get(i);
			price = line[3];
			qty = line[2];
			
			if (isDouble(price)) {
				total = Double.valueOf(price) + total;
			} 
//			qty = MoleQUtil.fixPrintQty(qty);
			if( MoleQUtil.isNumeric2(qty)){
				iQty = Double.valueOf(qty);
				totalqty = (int)iQty + totalqty;
			}
		}
		
		result += "\n ";
		result += SetSpaceE(NAME, "Total");
		result += SetSpaceB(QTY, MoleQUtil.fixPrintQty(String.valueOf(totalqty)));
		result += SetSpaceC(AMT, showQty?MoleQUtil.fixPrintPrice(total)+" ":"");
		
		return result ;
	}
		
	
	public String getFooter(String rptName,boolean showAmt){
		String result = "";
		result += "\n";
		result += "-----------------------------------------------";
		result += getTotlal(showAmt);
		result += " \n";
		
		return result;
	}
	
//	public void sort(){
//		Collections.sort(m_report, new ReportComparator(ReportComparator.UP)); 
//	}
		
	public String toPrintReport() {
		String result = "";
		String qty = "";
		if (m_report != null) {
			
//			this.sort();
			
			for (int i = 0; i < m_report.size(); i++) {
				String[] row = m_report.get(i);
			
				String name = cutPoint(row[1]);
				
				if (!row[3].trim().equals("") && isDouble(row[3])) {
					double amt = Double.valueOf(row[3]);
					if (amt != 0) {
						result += "\n ";
						result += SetSpaceE(NAME, name);
						// result += SetSpaceB(QTY,
						// MoleQUtil.fixPrintQty(row[2]).equals("0")?"":MoleQUtil.fixPrintQty(row[2]));
						result += SetSpaceB(QTY, MoleQUtil.fixPrintQty(row[2])
								.equals("0") ? MoleQUtil.fixPrintQty("0")
								: MoleQUtil.fixPrintQty(row[2]));
						result += SetSpaceC(AMT,row[3]);
					} else {
						if (MoleQUtil.isNumeric2(row[2])) {
							int rptqty = Integer.valueOf(row[2]);
							result += "\n ";
							result += SetSpaceE(NAME, name);
							// result += SetSpaceB(QTY,rptqty==0?"":String.valueOf(rptqty));
							result += SetSpaceB(QTY,
									rptqty == 0 ? MoleQUtil.fixPrintQty("0")
											: String.valueOf(rptqty));
							// result += SetSpaceC(AMT, "");
							result += SetSpaceC(AMT, MoleQUtil.fixPrintPrice(0));
						}
					}
				} else {
					result += "\n ";
					result += SetSpaceE(NAME, name);
					result += SetSpaceB(QTY, MoleQUtil.fixPrintQty(row[2])
							.equals("0") ? "" : MoleQUtil.fixPrintQty(row[2]));
					if (!isDouble(row[3]) && !row[3].equals(" ")) {
						if (isDouble(row[3].trim())) {
							result += SetSpaceC(AMT, row[3]);
						} else {
							result += " " + row[3];
						}

					} else {
						result += SetSpaceC(AMT, "");
					}

				}
//				result += "\n";
			}

		}

		return "" + result + "";
	}
	
	public String toPrintInventory() {
		String result = "";
		String qty = "";
		if (m_report != null) {
			
//			this.sort();
			
			for (int i = 0; i < m_report.size(); i++) {
				String[] row = m_report.get(i);
			
				String name = cutPoint(row[1]);
				
				if (!row[2].trim().equals("") && MoleQUtil.isNumeric2(row[2])) {
				
					result += "\n ";
					result += SetSpaceE(NAME, name);
					result += SetSpaceB(QTY, MoleQUtil.fixPrintQty(row[2]));
					result += SetSpaceC(AMT, "");
				}else{
					result += "\n ";
					result += SetSpaceE(NAME, name);
					result += SetSpaceB(QTY, "");
					result += SetSpaceC(AMT, "");
				}

			}
		}

		return "" + result + "";
	}

	public String SetSpaceB(int MaxSize, String Context) {
		String result = "";
		int space = MaxSize - Context.length();
		if (space > 0) {
			for (int j = 0; j < space; j++) {
				result += " ";
			}
			result += Context;
		} else {
			result = Context.substring(0, MaxSize);
		}
		return result;
	}
	
	public String SetSpaceC(int MaxSize, String Context) {
		String result = "";
		int space = MaxSize - Context.length();
		if (space > 0) {
			for (int j = 0; j < space; j++) {
				result += " ";
			}
			result += Context;
		} else {
			result = Context.substring(0, MaxSize);
		}
		return result;
	}

	public String SetSpaceE(int MaxSize, String Context) {
		String result = "";
		int space = MaxSize - Context.length();
		if (space > 0) {
			result = Context;
			for (int j = 0; j < space; j++) {
				result += " ";
			}
		} else {
			result = Context.substring(0, MaxSize);
		}
		return result;
	}
	
	public boolean isDouble(String str) {
		Pattern pattern = Pattern.compile("^[-\\+]?[.\\d]*$");
		return pattern.matcher(str).matches();
	}
	
//	public static boolean isNumeric(String str){ 
//		for (int i = str.length();--i>=0;){ 
//		if (!Character.isDigit(str.charAt(i))){
//		return false; 
//		} 
//		}
//		return true; 
//		}
	
	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public String getmReportName() {
		return mReportName;
	}

	public void setmReportName(String mReportName) {
		this.mReportName = mReportName;
	}

	public String toPrintContent() {
		String result = "";
		
		
		
		return result;
	}

	public String toPrintContent(String str) {
//		PosPrinter printer = (PosPrinter) DeviceManager.getInstance().printer;
		String result ="";
//		String[] tmp1 = str.split("\n");
//		for (int i = 0; i < tmp1.length; i++) {
//			result += printer.toPrintContent(tmp1[i]);
//		}
		return result;
	}
}
